#include "playscene.h"
#include<QDebug>
#include<QMenuBar>
#include<QPainter>
#include"mypushbutton.h"
#include<QTimer>
#include<QLabel>
#include"mycoin.h"
#include"dataconfig.h"
#include<QPropertyAnimation>
PlayScene::PlayScene(int index)
{
    this->levelIndex=index;
    qDebug()<<"您进入的是第"<<this->levelIndex<<"关";

    //设置窗口尺寸
    this->setFixedSize(320,588);

    //设置标题
    this->setWindowTitle("翻金币游玩场景");
    //设置图标
    this->setWindowIcon(QIcon(":/new/prefix1/res/Coin0001.png"));
    //设置菜单栏
    QMenuBar* bar=menuBar();
    this->setMenuBar(bar);
    //创建菜单
    QMenu* startMenu=bar->addMenu("开始");
    QAction* quitAction=startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });


    //返回按钮
    MyPushButton*backBtn=new MyPushButton(":/new/prefix1/res/BackButton.png",":/new/prefix1/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    //监听按下
    connect(backBtn,&MyPushButton::clicked,[=](){
       //发送点击返回的信号
       //可以进行延时，这样点击back的时候跳回主场景不会太快
        QTimer::singleShot(500,this,[=](){
            emit this->chooseSceneBck();
            //谁来监听呢，在第二场景监听
         });

    });

    //显示当前关卡号
    QLabel *label=new QLabel;
    label->setParent(this);
    QString str=QString("Level:%1").arg(this->levelIndex);
    label->setText((str));
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    //设置标签的大小和位置
    label->setGeometry(QRect(30,this->height()-50,this->width(),50));
    //设置属性
    label->setAttribute(Qt::WA_TransparentForMouseEvents);

    //胜利图片
    QLabel* winLabel=new QLabel;
    winLabel->setParent(this);
    QPixmap pix;
    pix.load(":/new/prefix1/res/LevelCompletedDialogBg.png");
    winLabel->setPixmap(pix);
    winLabel->setGeometry(0,0,pix.width(),pix.height());
    winLabel->move(this->width()*0.5-pix.width()*0.5,-pix.height());


    dataConfig config;
    //初始化关卡二维数组
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
        }
    }

    //初始化背景
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            QLabel* label=new QLabel;
            QPixmap pix;
            pix.load(":/new/prefix1/res/BoardNode.png");
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(57+i*pix.width(),200+j*pix.height());

            //创建金币
            QString str;
            if(this->gameArray[i][j]==1){
                str=":/new/prefix1/res/Coin0001.png";
            }
            else if(this->gameArray[i][j]==0){
                str=":/new/prefix1/res/Coin0008.png";
            }
            MyCoin*coin=new MyCoin(str);
            coin->setParent(this);
            coin->move(59+i*pix.width(),204+j*pix.height());
            //设置coin属性
            coin->posX=i;
            coin->posY=j;
            coin->flag=gameArray[i][j];

            this->coinBtn[i][j]=coin;

            //监听点击信号，changeflage
            connect(coin,&MyCoin::clicked,[=](){
                coin->changeFlag();
                //同步二维数组，可以后期拓展功能，如读档等
                gameArray[i][j]=gameArray[i][j]==0?1:0;

                 for(int i=0;i<4;i++){
                     for(int j=0;j<4;j++){
                         this->coinBtn[i][j]->isWin=true;
                     }
                 }

                //开始反转周围金币

                //实现延时反转周围硬币
                QTimer::singleShot(300,this,[=](){
                    //右
                    if(coin->posX+1<=3){
                        this->coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        gameArray[coin->posX+1][coin->posY]=gameArray[coin->posX+1][coin->posY]==0?1:0;
                    }

                    //左
                    if(coin->posX-1>=0){
                        this->coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        gameArray[coin->posX-1][coin->posY]=gameArray[coin->posX-1][coin->posY]==0?1:0;
                    }

                    //下
                    if(coin->posY+1<=3){
                        this->coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        gameArray[coin->posX][coin->posY+1]=gameArray[coin->posX][coin->posY+1]==0?1:0;
                    }
                    //上
                    if(coin->posY-1>=0){
                        this->coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        gameArray[coin->posX][coin->posY-1]=gameArray[coin->posX][coin->posY-1]==0?1:0;
                    }

                    //延时反转结束，检测游戏是否胜利
                    this->isWin=true;
                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++){
                            if(this->coinBtn[i][j]->flag==false){
                                this->isWin=false;
                                break;
                            }
                        }
                    }
                    if(this->isWin){
                        qDebug()<<"游戏胜利";
                        for(int i=0;i<4;i++){
                            for(int j=0;j<4;j++){
                                this->coinBtn[i][j]->isWin=true;
                            }}

                        // //将胜利图片显示
                         QPropertyAnimation*animation=new QPropertyAnimation(winLabel,"geometry");
                         animation->setDuration(1000);
                         animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                         animation->setEndValue(QRect(winLabel->x(),winLabel->y()+200,winLabel->width(),winLabel->height()));
                         //设置弹跳效果
                         animation->setEasingCurve(QEasingCurve::OutBounce);
                         animation->start(QAbstractAnimation::DeleteWhenStopped);
                    }
                    //是为了配合上面的禁用，这样就避免了延时产生的bug
                     else{
                         for(int i=0;i<4;i++){
                             for(int j=0;j<4;j++){
                                 this->coinBtn[i][j]->isWin=false;
                             }
                         }
                     }

                });


            });
        }
    }

}

void PlayScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/new/prefix1/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/new/prefix1/res/Title.png");
    //缩放
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);

}
